local rensheng = fk.CreateSkill {
  name = "cf_rensheng",
}

Fk:loadTranslationTable {
  ["cf_rensheng"] = "人胜",
  [":cf_rensheng"] = "每回合限一次，出牌阶段，你可以选择执行前任意项：1.摸两张牌，你使用牌堆顶的一张牌【蓄谋】；2.弃置两张牌，然后选择至多两张名其他角色的区域内牌并将它们依次【蓄谋】；3.回复一点体力并摸一张牌，然后你依次使用牌堆顶和牌堆底各一张牌【蓄谋】。你的手牌上限+X（X为你的蓄谋牌数）。",
  ["#cf_rensheng_prompt"] = "选择执行前任意项",
  ["#cf_rensheng_prompt1"] = "选择一名角色",
  ["#cf_rensheng_prompt2"] = "选择该角色区域内的一张牌牌【蓄谋】"
}

local U = require("packages.utility.utility")

rensheng:addEffect("active", {
  can_use = function(self, player)
    return player:hasSkill(rensheng.name) and player:usedEffectTimes(self.name, Player.HistoryTurn) == 0
  end,
  card_num = 0,
  target_num = 0,
  on_cost = function(self, player, data, extra_data)
    local room = player.room
    local chioce = room:askToChoice(player, {
      choices = { "1", "2", "3", },
      cancelable = false,
      skill_name = rensheng.name,
      prompt = "#cf_rensheng_prompt",
    })
    return { chioce = chioce }
  end,
  on_use = function(self, room, skillUseEvent)
    local choice = tonumber(skillUseEvent.cost_data.chioce)
    local player = skillUseEvent.from
    if choice >= 1 then
      player:drawCards(2, rensheng.name)
      U.premeditate(player, room:getNCards(1), rensheng.name)
    end
    if choice >= 2 then
      room:askToDiscard(player, {
        max_num = 2,
        min_num = 2,
        skill_name = rensheng.name,
        cancelable = false,
        include_equip = true,
      })
      for i = 1, 2 do
        local targets = table.filter(room:getOtherPlayers(player, false), function(p)
          return not p:isNude()
        end)
        if #targets == 0 then break end
        local target = room:askToChoosePlayers(player, {
          targets = targets,
          max_num = 1,
          min_num = 1,
          skill_name = rensheng.name,
          cancelable = false,
          prompt = "#cf_rensheng_prompt1"
        })[1]
        local card = room:askToChooseCard(player, {
          target = target,
          flag = "hej",
          prompt = "#cf_rensheng_prompt2",
          skill_name = rensheng.name,
        })
        U.premeditate(player, card, rensheng.name)
      end
    end
    if choice == 3 then
      room:recover {
        who = player,
        num = 1,
        skillName = rensheng.name,
        recoverBy = player,
      }
      player:drawCards(1)
      U.premeditate(player, room:getNCards(1), rensheng.name)
      U.premeditate(player, room:getNCards(1, "bottom"), rensheng.name)
    end
  end
})

rensheng:addEffect("maxcards", {
  correct_func = function(self, player)
    local n = #table.filter(player:getCardIds("j"), function(element, index, array)
      return player:getVirtualEquip(element) and player:getVirtualEquip(element).name == "premeditate"
    end)
    return n or 0
  end
})

return rensheng
